package cn.edu.nju.ws.sview.wyge.relations.adataset.overview;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.LeanA;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.LeanS;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.concept;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.conceptHierarchy;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.triple;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.tripleInfo;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.typeInfo;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataMake.dataMake;

public class GetDotGraph {

	/**
	 * @param args
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	static void print(int i, int k, ArrayList<concept> concepts,
			HashSet<String> hs, PrintWriter out) {
		if (concepts.get(k).state == concept.Associated_S) {
			String s = i + " " + k;
			if (!hs.contains(s)) {
				hs.add(s);
				out.println("concept" + i + "->" + "concept" + k + ";");
			}
			return;
		}
		Object ob[] = concepts.get(k).father.toArray();
		for (int j = 0; j < ob.length; ++j)
			print(i, Integer.parseInt(ob[j].toString()), concepts, hs, out);
	}

	public static void createFromDocumentID(ArrayList<Integer> documents,
			String filename) throws IOException, ClassNotFoundException,
			SQLException {
		// 求sview中所有的concept的信息，这里concept是无序的
		ArrayList<concept> concepts = dataMake.getAllClassInfo(documents);

		// 把concepts变为有序的
		conceptHierarchy.getConcepts(concepts);

		// 求每一个id对应的concepts中的位置
		// 求出所有实例的类型
		HashMap<String, Integer> instanceIdToIndex = typeInfo.getType(concepts,
				documents);
		// 求出A
		ArrayList<triple> triples = tripleInfo.getTriple(instanceIdToIndex,
				documents);
		/*
		 * for (int i = 0; i < concepts.size(); ++i){ System.out.println(i + " "
		 * + concepts.get(i).label + " " + concepts.get(i).id); Object ob[] =
		 * concepts.get(i).father.toArray(); System.out.print("father:    ");
		 * for (int j = 0; j < ob.length; ++j)
		 * System.out.print(concepts.get((Integer)ob[j]).label + " ");
		 * System.out.println(); ob = concepts.get(i).ancestor.toArray();
		 * System.out.print("ancestor:    "); for (int j = 0; j < ob.length;
		 * ++j) System.out.print(concepts.get((Integer)ob[j]).label + " ");
		 * System.out.println(); }
		 */
		// 求lean_A
		ArrayList<triple> la = LeanA.get(concepts, triples);
		// 求lean_S
		LeanS.get(concepts, la);
		/**/
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				filename)));
		out.println("digraph G {");
		out.println("node [fontname=\"yahei.ttf\"];");
		out.println("edge [fontname=\"yahei.ttf\"];");
		Object ob[] = la.toArray();
		for (int i = 0; i < ob.length; ++i) {
			triple tri = (triple) ob[i];
			out.println("\"" + concepts.get(tri.from).uri + "\"" + "->" + "\""
					+ concepts.get(tri.to).uri + "\"" + "[label=\"" + tri.prop
					+ "\", color=blue, fontcolor=blue];");
			/*
			 * out.println("concept" + tri.from + "->" + "concept" + tri.to +
			 * "[label=\"" + tri.prop + "\", color=blue, fontcolor=blue];");
			 */
		}
		out.println("edge [fontname=\"yahei.ttf\" arrowhead=\"empty\"];");
		// out.println("digraph G {");
		for (int i = 0; i < concepts.size(); ++i)
			if (concepts.get(i).state == concept.Associated_S)
				out.println("\"" + concepts.get(i).uri + "\""
						+ " [shape=box,label=\"" + concepts.get(i).label
						+ "\"];");
		HashSet<String> hs = new HashSet<String>();
		for (int i = 1; i < concepts.size(); ++i)
			if (concepts.get(i).state == concept.Associated_S) {
				ob = concepts.get(i).father.toArray();
				for (int j = 0; j < ob.length; ++j) {
					int k = Integer.parseInt(ob[j].toString());
					print(i, k, concepts, hs, out);
					// if (k != 0) out.println("concept" + i + "->" + "concept"
					// + k
					// + ";");
				}
			}
		out.println("}");
		out.close();
	}

	public static void get(String tmpDirectory, String ds_type, int ds_id)
			throws ClassNotFoundException, SQLException, IOException {
		ArrayList<Integer> documents = new ArrayList<Integer>();
		if (ds_type.equals("uri")) {
			documents.add(ds_id);
		} else {
			Connection con = DBConnectionFactory.getConnection();
			PreparedStatement seldocs = con
					.prepareStatement("SELECT doc_id FROM custom_dataset_doc where dataset_id = ?");
			seldocs.setInt(1, ds_id);
			ResultSet seldocsrs = seldocs.executeQuery();
			while (seldocsrs.next()) {
				documents.add(seldocsrs.getInt(1));
			}
			seldocsrs.close();
			con.close();
		}
		createFromDocumentID(documents, tmpDirectory + ds_type + ds_id + ".dot");
	}
}
